package pers.chao.tool.http.client;

public class AsyncHttpClientConfig {

	/**
	 * 创建连接超时时间
	 */
	private int connectTimeout;

	/**
	 * http请求超时时间
	 */
	private int requestTimeout;

	/**
	 * IO线程数
	 */
	private int ioThreads;

	/**
	 * {@link AsyncHttpClient}会根据IP和Port缓存连接, 该参数指定一个IP+Port对应的最大连接数
	 */
	private int maxConnectionsEachServer;

	public AsyncHttpClientConfig() {
	}

	public AsyncHttpClientConfig(int connectTimeout, int requestTimeout, int ioThreads, int maxConnectionsEachServer) {
		this.connectTimeout = connectTimeout;
		this.requestTimeout = requestTimeout;
		this.ioThreads = ioThreads;
		this.maxConnectionsEachServer = maxConnectionsEachServer;
	}

	public int getConnectTimeout() {
		return connectTimeout;
	}

	public void setConnectTimeout(int connectTimeout) {
		this.connectTimeout = connectTimeout;
	}

	public int getRequestTimeout() {
		return requestTimeout;
	}

	public void setRequestTimeout(int requestTimeout) {
		this.requestTimeout = requestTimeout;
	}

	public int getIoThreads() {
		return ioThreads;
	}

	public void setIoThreads(int ioThreads) {
		this.ioThreads = ioThreads;
	}

	public int getMaxConnectionsEachServer() {
		return maxConnectionsEachServer;
	}

	public void setMaxConnectionsEachServer(int maxConnectionsEachServer) {
		this.maxConnectionsEachServer = maxConnectionsEachServer;
	}

	/**
	 * 默认配置
	 * @return
	 */
	public static AsyncHttpClientConfig defaultConfig() {
		return AsyncHttpClientConfig.builder()
				.setConnectTimeout(60 * 1000)
				.setRequestTimeout(30 * 1000)
				.setIoThreads(Runtime.getRuntime().availableProcessors() * 2)
				.setMaxConnectionsEachServer(Runtime.getRuntime().availableProcessors() * 2)
				.build();
	}

	/**
	 * 自定义默认配置
	 * @param ioThreads
	 * @param maxConnectionsEachServer
	 * @return
	 */
	public static AsyncHttpClientConfig defaultConfig(int ioThreads, int maxConnectionsEachServer) {
		return AsyncHttpClientConfig.builder()
				.setConnectTimeout(60 * 1000)
				.setRequestTimeout(30 * 1000)
				.setIoThreads(ioThreads)
				.setMaxConnectionsEachServer(maxConnectionsEachServer)
				.build();
	}

	public static AsyncHttpClientConfigBuilder builder() {
		return new AsyncHttpClientConfigBuilder();
	}

	public static class AsyncHttpClientConfigBuilder {
		private int connectTimeout;
		private int requestTimeout;
		private int ioThreads;
		private int maxConnectionsEachServer;

		public AsyncHttpClientConfigBuilder setConnectTimeout(int connectTimeout) {
			this.connectTimeout = connectTimeout;
			return this;
		}

		public AsyncHttpClientConfigBuilder setRequestTimeout(int requestTimeout) {
			this.requestTimeout = requestTimeout;
			return this;
		}

		public AsyncHttpClientConfigBuilder setIoThreads(int ioThreads) {
			this.ioThreads = ioThreads;
			return this;
		}

		public AsyncHttpClientConfigBuilder setMaxConnectionsEachServer(int maxConnectionsEachServer) {
			this.maxConnectionsEachServer = maxConnectionsEachServer;
			return this;
		}

		public AsyncHttpClientConfig build() {
			return new AsyncHttpClientConfig(connectTimeout, requestTimeout, ioThreads, maxConnectionsEachServer);
		}
	}
}
